#include <stdio.h>
#include <stdlib.h>
#include <config.h>
#include <unistd.h>
#include <fcntl.h>
#include <jimix/Process.h>
#include <jimix/Globals.h>
#include <jimix/Directory.h>
#include <jimix/Filesystem.h>
#include <jimix/ProcessManager.h>
#include <sys/dirent.h>
#include <jimix/resource.h>
#include <signal.h>

extern "C" int yield();
extern u32int _process;

int main(char argc, char **argv)
{
  char **null = 
    {
      NULL
    };

  int ret = fork();
  if (ret == 0)
  {
    execve("/udev",null,null);
        asm volatile("int $3");
  }

  u32int filesystem = requestResource("Filesystem");
  u32int f;
  do 
  {
    yield();
    f = callResource(filesystem, Filesystem_IF::traverse, "/dev/null");
  }
  while (f == NULL);

  ret = fork();
  if (ret == 0)
    execve("/tty",null,null);

  do
  {
    yield();
    f = callResource(filesystem, Filesystem_IF::traverse, "/dev/tty1");
  }
  while (f == NULL);
  
  callResource(_process, Process_IF::setCtty, "/dev/tty1");

  ret = fork();
  if (ret == 0)
      execve("/shell", null, null);
  
  do
  {
    yield();
    f = callResource(filesystem, Filesystem_IF::traverse, "/dev/ttyS1");
  }
  while (f == NULL);
  
  callResource(_process, Process_IF::setCtty, "/dev/ttyS1");
  
  ret = fork();
  if (ret == 0)
      execve("/shell", null, null);
  
   ret = fork();
   if (ret == 0)
     execve("/ata",null,null);
   
   do
   {
     yield();
     f = callResource(filesystem, Filesystem_IF::traverse, "/dev/hda1");
   }
   while (f == NULL);
   
   
   ret = fork();
   if (ret == 0)
     execve("/ext2", null, null);
   
   do
   {
     yield();
     f = callResource(filesystem, Filesystem_IF::traverse, "/mnt/bin/bash");
   }
   while (f == NULL);

//   FILE *stream = fopen("/mnt/bin/bash", "r");
//   if (stream)
//   {
//     u8int buf[128];
//     fread(buf, 128, 1, stream);
//     fclose(stream);
//     u32int acc = 0;
//     for (int i = 0; i < 128; i++)
//       acc += buf[i];
//   
//     FILE *out = fopen("/dev/tty", "w");
//     fprintf(out, "Accumulator = %x\n", acc);
//     fclose(out);
//   }
//   else
//   {
//     FILE *out = fopen("/dev/tty", "w");
//     fprintf(out, "Couldn't find /mnt/bash!");
//     fclose(out);
//   }
  
//    ret = fork();
//    if (ret == 0)
//      execve("/mnt/bin/bash", null, null);
  
for(;;);
  // 
//   File *hda;
//   hda = filesystem->traverse("/dev/hda1");
//   while (!(hda=filesystem->traverse("/dev/hda1")));
//   ret = fork();
//   if (ret == 0)
//   {
//     execve("/ext2",null,null);
//   }
// 
//    FILE *file = fopen("/dev/tty1", "w");
//   fprintf(file, "rar!\n");
//   fflush(file);
//    
//   Directory *dir = (Directory*)filesystem->traverse("/mnt");
//   while (!dir || !dir->isMountpoint())
//   {
//     dir = (Directory*)filesystem->traverse("/mnt");
//   }
// //  fprintf(file, "dir = 0x%x\n", dir);
//   fflush(file);

//  fprintf(file, "N entries = %d\n", dir->getNEntries());

//   DIR *dir2 = opendir("/mnt/bin");
//   struct dirent *dirent;
//   while ( (dirent=readdir(dir2)) != NULL )
//   {
//     fprintf(file, "Name: %s\n", dirent->d_name);
//   }
//   closedir(dir2);

//   int stream = open("/mnt/bin/bash", 0);
//   //seekl(stream, 0, SEEK_END);
//   //int sz = ftell(stream);
//   //fprintf(file, "Sz: %d\n", sz);
//   int sz = 1024 * 512;
//   lseek(stream, 1024*0, SEEK_SET);
//   unsigned char *buf = new unsigned char[sz];
//   fprintf(file, "Reading...\n");
//   fflush(file);
//   read(stream, buf, sz);
//   fprintf(file, "Got data.\n");
//   fflush(file);
//   unsigned int acc = 0;
//   for (int i = 0; i < sz; i++)
//   {
//     acc += buf[i];
//   }
//   fprintf(file, "Accumulator: %x\n",acc);
//   close(stream);
  
//   ret = fork();
//   if (ret == 0)
//   {
//     char *argv[] = {
//       "/mnt/bin/bash",
//       "-i",
//       NULL
//     };
//     execve("/mnt/bin/bash",null,null);
//   }
//   fprintf(file, "Size: %x\n", u);
  
//   for(;;);
//   fclose(file);

  for(;;)
    yield();
}
